package com.hd123.rumba.gwt.widget.client.codenamebox;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.hd123.rumba.gwt.widget.client.LabeledTextBox;
import com.hd123.rumba.gwt.widget.client.grid.DxGrid;
import com.hd123.rumba.gwt.widget.client.grid.DxGridColumnDef;
import com.hd123.rumba.gwt.widget.client.grid.DxGridListener;
import com.hd123.rumba.gwt.widget.client.grid.DxNavGrid;
import com.hd123.rumba.gwt.widget.client.grid.StringRendererFactory;

public class CNBBrowser extends PopupPanel implements KeyboardListener, DxGridListener,
    ChangeListener {
  CNBBrowserDataProvider provider;
  DxGrid grid;
  DxNavGrid navgrid;
  VerticalPanel panel = new VerticalPanel();
  LabeledTextBox code = new LabeledTextBox("代码");
  LabeledTextBox name = new LabeledTextBox("名称");
  public Object selectedObject = null;

  public CNBBrowser(CNBBrowserDataProvider provider) {
    super(true, false);
    panel.setWidth("200px");
    panel.setStyleName("jdgwt-CNBBrowser");

    this.provider = provider;
    grid = new DxGrid(1, 2);
    grid.setProvider(provider);
    grid.setWidth("100%");
    grid.setCompact(true);
    DxGridColumnDef cd;
    grid.addColumnDef(cd = new DxGridColumnDef("代码"));
    cd.setRendererFactory(new StringRendererFactory());
    grid.addColumnDef(cd = new DxGridColumnDef("名称"));
    cd.setRendererFactory(new StringRendererFactory());

    navgrid = new DxNavGrid(provider, grid);
    navgrid.setWidth("100%");
    panel.add(navgrid);

    HorizontalPanel hp = new HorizontalPanel();
    hp.setStyleName("jdgwt-CNBBrowser-InputPanel");
    hp.add(code);
    code.getLabel().setWidth("30px");
    hp.add(name);
    name.getLabel().setWidth("30px");
    panel.add(hp);

    setWidget(panel);

    grid.addKeyboardListener(this);
    grid.addDxGridListener(this);
    code.addChangeListener(this);
    name.addChangeListener(this);
  }

  public void onLoad() {
    selectedObject = null;
    provider.setNeedFetch(true);
    grid.clear();
    navgrid.gotoPage(provider.getCurrentPage());
  }

  public void onKeyDown(Widget sender, char keyCode, int modifiers) {
  }

  public void onKeyPress(Widget sender, char keyCode, int modifiers) {
    if (sender == grid) {
      if (keyCode == KEY_ENTER) {
        if (grid.getCurrentRow() < provider.getRowCount()) {
          selectedObject = provider.getValues().get(grid.getCurrentRow());
          hide();
        }
      } else if (keyCode == KEY_ESCAPE) {
        selectedObject = null;
        hide();
      }
    }
  }

  public void onKeyUp(Widget sender, char keyCode, int modifiers) {
  }

  public void afterRefresh(DxGrid theGrid) {
    theGrid.setFocus(true);
  }

  public void onCellDblClick(DxGrid theGrid, int row, int col) {
    if (row < provider.getRowCount()) {
      selectedObject = provider.getValues().get(row);
      hide();
    }
  }

  public void onChange(Widget sender) {
    if (sender == code) {
      provider.setFilter(new CNBFilter(code.getText(), null).carry(provider.getFilter()));
      navgrid.gotoPage(0);
    } else if (sender == name) {
      provider.setFilter(new CNBFilter(null, name.getText()).carry(provider.getFilter()));
      navgrid.gotoPage(0);
    }
  }
}
